import requests,json
from bs4 import BeautifulSoup

# 目标URL
url = "https://wiki.biligame.com/sr/%E5%BC%80%E6%8B%93%E4%BB%BB%E5%8A%A1"

# 发送GET请求
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0 Safari/537.36"
}
response = requests.get(url, headers=headers)

information_book = {}

# 检查请求是否成功
if response.status_code == 200:
    # 解析网页内容
    soup = BeautifulSoup(response.text, "html.parser")
    
    # 找到所有class为drop-down-wrap的div
    divs = soup.find_all("div", class_="drop-down-wrap")
    
    # 输出每个div的内容
    for i, div in enumerate(divs, 1):
        title = div.find("div", class_="wrap-title")
        content = div.find("div", class_="wrap-content")
        title_text = title.find("a").text
        contentlist = content.find_all("a")
        information_book[title_text] = []
        for j in contentlist:
            if j.find("img")==None:
                information_book[title_text].append(j.prettify())
        
        print(f"第{i}个drop-down-wrap div内容：")
        print(title_text)
        #print("内容列表：")
        #for item in information_book[title_text]:
        #    print(item.text)
        print("="*50)
    
    with open("information_book.json", "w", encoding="utf-8") as f:
        json.dump(information_book, f, ensure_ascii=False, indent=4)
    
else:
    print(f"请求失败，状态码：{response.status_code}")
